package com.nf.health.app.mydoctor.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.mydoctor.adapter.GzDoctorAdapter;
import com.nf.health.app.adapter.SearchDoctorAdapter;
import com.nf.health.app.customview.ClearEditText;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.mydoctor.fragment.AttentionGroupFragment;
import com.nf.health.app.mydoctor.fragment.DoctorFragment;
import com.nf.health.app.models.DoctorVo;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.zxing.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author 于亚豪
 * @ClassName: MyDoctorNewActivity
 * @Description: 我的医生页面  新
 * @date
 */
public class MyNewDoctorActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener ,View.OnClickListener {


    @Bind(R.id.doctor_search)
    ClearEditText doctorSearch;
    @Bind(R.id.rg_history)
    RadioGroup rg_history;

    /**
     * 记录按下的页签索引
     */
    private int tab = 0;
    public static final int index_search_fragment = 0;// 加入的群
    public static final int index_persional = 0;// 私人医生
    public static final int index_enjoy_group = 2;// 加入的群

    private int currentTabIndex;
    private DoctorFragment  fragment2;
    private AttentionGroupFragment  fragment3;
    //private RadioGroup rg_history;


    private int page = 1;
    private int page_size = 20;

    GzDoctorAdapter adapterDoctor;
    SearchDoctorAdapter searchDoctorAdapter;
    private FragmentTransaction transaction;

    private FragmentManager fragmentManager;// 用于对Fragment进行管理

    /***************搜索listView *******/
    @Bind(R.id.listview_doctor_serach)
    DragListView listview_doctor_serach;
    /**************主要内容存放Fragment******/
    @Bind(R.id.id_content)
    FrameLayout id_content;
    /***********搜索存放的集合*******/
    private  List<DoctorVo> serchDocList = null;
    private ImageView right_view;
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_my_new_doctor);
        ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        //rg_history = ViewUtil.getView(this, R.id.rg_history);
        rg_history.setOnCheckedChangeListener(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "我的医生");
        right_view = (ImageView)findViewById(R.id.titlebar_right_view);
        right_view.setImageResource(R.drawable.home_scan);
        right_view.setVisibility(View.VISIBLE);
        listview_doctor_serach.setVisibility(View.GONE);
        serchDocList = new ArrayList<DoctorVo>();
        right_view.setOnClickListener(this);
       if (arg0 != null) {
            setTabSelection(arg0.getInt("tab", index_persional));
        } else {
            setTabSelection(index_persional);
        }


        doctorSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int count, int after) {
                if(doctorSearch.isHasFoucs()){
                    doctorSearch.setClearIconVisible(s.length() > 0);
                }
                if(listview_doctor_serach.getVisibility() == View.GONE){
                    listview_doctor_serach.setVisibility(View.VISIBLE);
                    id_content.setVisibility(View.GONE);
                }
//                if(s.length()>0){
                    provider.requestSearchDoctor2(doctorSearch.getText().toString(), "attention", page + "", page_size + "", "searchDoctor");
//                }
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {

            }

            @Override
            public void afterTextChanged(Editable arg0) {

            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
            listview_doctor_serach.setVisibility(View.GONE);
            id_content.setVisibility(View.VISIBLE);
        switch (checkedId) {
            case R.id.rb_no_reply://私人医生
                tab = index_persional;
                setTabSelection(index_persional);
                break;
            case R.id.rb_finish:// 加入的群
                tab = index_enjoy_group;
                setTabSelection(index_enjoy_group);
                break;
        }
    }
    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     */
    private void setTabSelection(int index) {
        // 开启一个Fragment事务
        transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case index_persional:
                if (fragment2 == null) {
                    fragment2 = new DoctorFragment();
                    transaction.add(R.id.id_content, fragment2);
                } else {
                    transaction.show(fragment2);
                }
                break;
            case index_enjoy_group:
                if (fragment3 == null) {
                    fragment3 = new AttentionGroupFragment();
                    transaction.add(R.id.id_content, fragment3);
                } else {
                    transaction.show(fragment3);
                }
                break;
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction
     *            用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (fragment2 != null) {
            transaction.hide(fragment2);
        }
        if (fragment3 != null) {
            transaction.hide(fragment3);
        }
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        // TODO Auto-generated method stub
        super.onAttachFragment(fragment);
        if (fragment2 == null && fragment instanceof DoctorFragment) {
            fragment2 = (DoctorFragment) fragment;
        }else if (fragment3 == null && fragment3 instanceof AttentionGroupFragment) {
            fragment3 = (AttentionGroupFragment) fragment;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", tab);
    }
    @SuppressWarnings("unchecked")
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if ("searchDoctor".equals(actionName)) {
            listview_doctor_serach.completeRefresh();
            List<DoctorVo> result = (List<DoctorVo>) object;
            serchDocList.clear();
            if (result.size() <= 0) {
                if (adapterDoctor != null) {
                    adapterDoctor.notifyDataSetChanged();
                    listview_doctor_serach.setAdapter(adapterDoctor);
                }
            } else {
                serchDocList.addAll(result);
                if (adapterDoctor == null) {
                    adapterDoctor = new GzDoctorAdapter(MyNewDoctorActivity.this,serchDocList);
                    listview_doctor_serach.setAdapter(adapterDoctor);
                } else {
                    adapterDoctor.notifyDataSetChanged();
                }
            }

        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_right_view:
                ActivityUtils.showActivityForResult(MyNewDoctorActivity.this, CaptureActivity.class, 100);
                break;

            case R.id.titlebar_back_view:
                finish();
                break;

            default:
                break;
        }
        super.onClick(v);
    }
}
